<?php

class Soul_Request_Http_Messager {

    private $_session = NULL;
    protected $sessionKey = 'httpmessages';

    public function __construct(){
        $this->_session = $GLOBALS[RC]->getSession();
        if(!$this->_session->has($this->sessionKey)){
        	$arr = array(
        		'success'=>array(),
        		'error'=>array(),
        		'notice'=>array(),
        		'information'=>array()
        	);
        	$this->_session->set($this->sessionKey, $arr);
        }
    }
    
    public function addInformation($content){
    	return $this->addMessage('information', $content);
    }
    
    public function getInformation(){
    	return $this->getMessage('information');
    }
    
    public function addNotice($content){
    	return $this->addMessage('notice', $content);
    }
    
    public function getNotice(){
    	return $this->getMessage('notice');
    }    

    public function addSuccess($content){
    	return $this->addMessage('success', $content);
    }
    
    public function getSuccess(){
        return $this->getMessage('success');
    }

    public function addError($content){
    	return $this->addMessage('error', $content);
    }
    
    public function getError(){
    	return $this->getMessage('error');
    }
    
    protected function addMessage($type, $msg){
    	$arr = $this->_session->get($this->sessionKey);
    	$arr[$type][] = $msg;
    	$this->_session->set($this->sessionKey, $arr);
    	return $this;
    }
    
    protected function getMessage($type){
    	$arr = $this->_session->get($this->sessionKey);
    	if(is_array($arr)){
	    	$result = $arr[$type];
	    	$arr[$type] = array();
	    	$this->_session->set($this->sessionKey, $arr);
	    	return $result;
    	}
    	return NULL;
    }    
}